//编辑或添加专题活动

window.onload=function(){
	init_addImage();
	changeBackgroundType();
}
$(".div_show").mouseover(function(){
	if( $(this).children("img").attr("src")&&$(this).children("img").attr("data-type")!="1"){
		$(this).find(".delete").show();
	}
  });
  $(".div_show").mouseout(function(){
	$(this).find(".delete").hide();
  });
$(".delete").click(function(){
		var fileinput=document.getElementById($(this).attr("data-id"));
		var img=$(this).parent().prev("img");
		var type=$(img).attr("data-type");
		layer.confirm("确定清空已选中的图片?",function(){
			layer.closeAll('dialog');
			if(type==2){
				img.removeAttr("src");
				img.removeAttr("data-type");
			}else if(type==3){
				img.attr("src",img.attr("data-oldsrc"));
				img.attr("data-type","1");
			}
			fileinput.outerHTML=fileinput.outerHTML;  
			init_addImage();
		});
});
function init_addImage(){
	/* 缩略图 */
	$(".addImage").change(function() {
		if (typeof (FileReader) != "undefined") {
			var dvPreview = $("#" + $(this).attr("data-id"));
			var regex = /(.jpg|.jpeg|.gif|.png|.bmp)$/;
			$($(this)[0].files).each(function() {
				var file = $(this);
				if (regex.test(file[0].name.toLowerCase())) {
					var reader = new FileReader();
					reader.onload = function(e) {
						var img =dvPreview.children("img");
						if(img.attr("src")&&img.attr("data-type")=='1'){
							img.attr("data-oldsrc", img.attr("src"));
							img.attr("data-type","3");
						}else{
							img.attr("data-type","2");
						}
						img.attr("src", e.target.result);			
						img.css({
							height : dvPreview.css("height"),
							width : dvPreview.css("width")
						});
					}
					reader.readAsDataURL(file[0]);
				} else {
					layer.alert(file[0].name + "不是图片文件!");
					var fileinput = document.getElementById(dvPreview.find('.delete').attr('data-id'));
					fileinput.outerHTML = fileinput.outerHTML;
					init_addImage();
					return false;
				}
			});
		} else {
			layer.alert("抱歉!浏览器版本太低了!不支持预览图片,换个浏览器试试吧~");
		}
	});
}
function changeBackgroundType(){
    var value=$("input[name='backgroundType']:checked").val();
	if(value=='2'){
	   $("#backgroundPicture_div").show();
	   $("#backgroundColor_div").hide();
	}else if(value=='1'){
	   $("#backgroundPicture_div").hide();
	   $("#backgroundColor_div").show();
	}
};

function sub_form(){
	var name = $("#name").val();
	if(name == null || name == ""){
	layer.alert("请输入专题名称");
		return false;
	}
	
	var shortName = $("#shortName").val();
	if(shortName == null || shortName == ""){
	layer.alert("请输入专题简称");	
		return false;
	}
	if(shortName.length > 5){
	layer.alert("专题简称不能超过5个字");	
		return false;
	}
	
	var startDate = $("#startDate").val();
	if(startDate == null || startDate == ""){
	layer.alert("请输入开始时间");	
		return false;
	}
	
	var endDate = $("#endDate").val();
	if(endDate == null || endDate == ""){
	layer.alert("请输入结束时间");
		return false;
	}
	
	
	var addMainPicture = $("#addMainPicture").val();
	var showMainPicture = $("#showMainPicture").children("img");
	//如果没有文件
	if ((addMainPicture.length <= 0 && showMainPicture.attr("data-type") != "1")
			|| showMainPicture.attr("src").length<=0) {
		layer.alert("请上传专题大图");
		return false;
	}
	
	var addBackgroundPicture = $("#addBackgroundPicture").val();
	var backgroundType = $("input[name='backgroundType']:checked").val();
	var backgroundColor = $("#backgroundColor").val();
	var backgroundPicture = $("#showBackgroundPicture").children("img");
	
	if(backgroundType == 2){
		if ((addBackgroundPicture.length <= 0 && backgroundPicture.attr("data-type") != "1")
				|| backgroundPicture.attr("src").length<=0) {
			layer.alert("请上传专题背景图");
			return false;
		}
	}
	var layer_load_window = layer.load(2, {time: 10*1000}); 
	$('#save_form').submit();
}
function callback(success,message){
	layer.closeAll('loading');
	if(success == true){
		window.location.href = path+ "/special_topic/to_add_template_special_topic_item?templateSpecialTopicId=" + message;
	}else{
		layer.alert(message);
	}
}
function cancle(){
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}
//js去掉空格
function trim(str){
    return  (str+"").replace(/^\s+|\s+$/g,"");
}